package main.java;

import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;

import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertEquals;

@FixMethodOrder(MethodSorters.JVM)
public class NGuessNumberTest {

    @Before
    public void generateAnswer() {
        String answer = NGuessNumber.generateAnswer();
        int length = answer.length();

        int count = 4;
        assertEquals(count, length);
        System.out.println("generateAnswer测试结果");
        System.out.println("答案数字数量为4，正确");

        Set<String> set = new HashSet<>();
        set.add(answer.substring(0, 1));
        set.add(answer.substring(1, 2));
        set.add(answer.substring(2, 3));
        set.add(answer.substring(3, 4));

        assertEquals(count, set.size());
        System.out.println("答案无重复数字，正确");

        boolean numFlag = true;
        for (String s : set) {
            Integer num = Integer.valueOf(s);
            if (num > 9 || num < 0) {
                numFlag = false;
                break;
            }
        }
        assertEquals(true, numFlag);
        System.out.println("答案数字都在0-9之间，正确\r\n");
    }

    @Before
    public void getPlayerInput() {
        System.out.println("getPlayerInput测试结果");
        assertEquals(4, NGuessNumber.playerInput.length);
        System.out.println("用户输入的数字数量为4，正确\r\n");
    }

    @org.junit.Test
    public void compareGuessAnswer() {
        String answer = NGuessNumber.generateAnswer();
        // 第一个入参手动输入测试
        String playerInput = "1234";
        String message = NGuessNumber.compareGuessAnswer(playerInput, answer);
        System.out.println("compareGuessAnswer测试结果");
        System.out.println("答案：" + answer + "，输入：" + playerInput + "，比较结果：" + message + "\r\n");
    }

    @After
    public void isWin() {
        String answer = NGuessNumber.generateAnswer();
        // 第一个入参手动输入测试
        String playerInput = "1234";
        String message = NGuessNumber.compareGuessAnswer(playerInput, answer);
        boolean win = NGuessNumber.isWin(message);
        System.out.println("isWin测试结果");
        if (win) {
            System.out.println("玩家获胜，返回：" + win);
        } else {
            System.out.println("玩家失败，返回：" + win);
        }
    }


//    public static void main(String[] args) {
//        String playerInput = NGuessNumber.getPlayerInput();
//        int length = playerInput.length();
//        int count = 4;
//        assertEquals(count, length);
//        System.out.println("数字数量为4，正确");
//    }
}